﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using RepositoryUtil.SQL;
using XRepair.Models;

namespace MvcXRepairCustomer.Repository.SQL
{
    public class SymptomDAO : BaseDAO
    {
        public IEnumerable<Symptom> GetAllActiveSymptoms()
        {
            string spName = "GetAllActiveSymptoms";

            DataTable resultTab = null;
            List<Symptom> symptomList = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;
                resultTab = command.ExecuteDataTable();
            }

            if (resultTab != null && resultTab.Rows.Count > 0)
            {
                symptomList = this.ConvertDataTableToSymptomList(resultTab);
            }

            return symptomList;
        }        

        private List<Symptom> ConvertDataTableToSymptomList(DataTable data)
        {
            List<Symptom> symptoms = null;

            if (data != null)
            {
                symptoms = new List<Symptom>();

                foreach (DataRow dr in data.Rows)
                {
                    var symptomModel = new Symptom();

                    symptomModel.ID = SqlUtil<int>.GetValue(dr, "ID");
                    symptomModel.Name = SqlUtil<string>.GetValue(dr, "Name");
                    symptomModel.Description = SqlUtil<string>.GetValue(dr, "Description");
                    symptomModel.BrandID = SqlUtil<int>.GetValue(dr, "BrandID");
                    symptomModel.ModelID = SqlUtil<int>.GetValue(dr, "ModelID");

                    symptoms.Add(symptomModel);
                }
            }

            return symptoms;
        }
    }
}
